package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Title: SetmealServiceImpl
 * @Package com.itheima.reggie.service.impl
 * @Author: 🐟
 * @Copyright 🐟
 * @CreateTime: 2022/9/12 11:39
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;


    @Override
    public void AddSetmealAndSetmealDishes(SetmealDto setmealDto) {
        setmealService.save(setmealDto);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getName, setmealDto.getName());
        Setmeal one = setmealService.getOne(lqw);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish dish : setmealDishes) {
            SetmealDish setmealDish = new SetmealDish();
            setmealDish.setName(dish.getName());
            setmealDish.setPrice(dish.getPrice());
            setmealDish.setDishId(dish.getDishId());
            setmealDish.setSetmealId(one.getId());
            setmealDish.setCopies(dish.getCopies());
            setmealDishService.save(setmealDish);
        }
    }

    @Override
    public void delectSetmeal(List<String> ids) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getStatus,1);
        lqw.in(Setmeal::getId,ids);
        int count = setmealService.count(lqw);
        if(count!=0){
            //业务异常
            throw new CustomException("该套餐处于启售状态，不能删除");
        }
        setmealService.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> lqw2 = new LambdaQueryWrapper<>();
        lqw2.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lqw2);
    }

    @Override
    public SetmealDto looksetmealdishes(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal byId = setmealService.getById(id);
        BeanUtils.copyProperties(byId,setmealDto);
        LambdaUpdateWrapper<SetmealDish> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(lqw);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto,setmeal);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmealService.updateById(setmeal);
        //修改套餐内的菜品，先删除，后添加
        LambdaUpdateWrapper<SetmealDish> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishService.remove(lqw);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> lsit = setmealDishes.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(lsit);
    }
}
